home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / lib / ruby / 1.8 / resolv-replace.rb < prev    next >
Text File  |  2008-08-07  |  2KB  |  64 lines

  1. require 'socket'
  2. require 'resolv'
  3.  
  4. class << IPSocket
  5.   alias original_resolv_getaddress getaddress
  6.   def getaddress(host)
  7.     begin
  8.       return Resolv.getaddress(host).to_s
  9.     rescue Resolv::ResolvError
  10.       raise SocketError, "Hostname not known: #{host}"
  11.     end
  12.   end
  13. end
  14.  
  15. class TCPSocket
  16.   alias original_resolv_initialize initialize
  17.   def initialize(host, serv, *rest)
  18.     rest[0] = IPSocket.getaddress(rest[0]) unless rest.empty?
  19.     original_resolv_initialize(IPSocket.getaddress(host), serv, *rest)
  20.   end
  21. end
  22.  
  23. class UDPSocket
  24.   alias original_resolv_bind bind
  25.   def bind(host, port)
  26.     host = IPSocket.getaddress(host) if host != ""
  27.     original_resolv_bind(host, port)
  28.   end
  29.  
  30.   alias original_resolv_connect connect
  31.   def connect(host, port)
  32.     original_resolv_connect(IPSocket.getaddress(host), port)
  33.   end
  34.  
  35.   alias original_resolv_send send
  36.   def send(mesg, flags, *rest)
  37.     if rest.length == 2
  38.       host, port = rest
  39.       begin
  40.         addrs = Resolv.getaddresses(host)
  41.       rescue Resolv::ResolvError
  42.         raise SocketError, "Hostname not known: #{host}"
  43.       end
  44.       err = nil
  45.       addrs[0...-1].each {|addr|
  46.         begin
  47.           return original_resolv_send(mesg, flags, addr, port)
  48.         rescue SystemCallError
  49.         end
  50.       }
  51.       original_resolv_send(mesg, flags, addrs[-1], port)
  52.     else
  53.       original_resolv_send(mesg, flags, *rest)
  54.     end
  55.   end
  56. end
  57.  
  58. class SOCKSSocket
  59.   alias original_resolv_initialize initialize
  60.   def initialize(host, serv)
  61.     original_resolv_initialize(IPSocket.getaddress(host), port)
  62.   end
  63. end if defined? SOCKSSocket
  64.